﻿using System;
using fastJSON;
using System.Linq;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkContract;
using Web.Areas.My.Models;
using SilenthinkContract.Domain;
using SilenthinkCommon.Extension;
using SilenthinkContract.Component;

namespace Web.Areas.My.Controllers
{
    public class AddressController : MyBaseController
    {
        public AddressController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            var model = new AddressControllerModel.Index();
            var identity = User.Identity.SkIdentity();

            var addresses = PassportService.GetUserAddresses(identity.Id);
            var locations = MemcachedClient.Put(CacheKey.Extra.Locations, BaseService.GetLocations);
            //var locations = BaseService.GetLocations();
            model.Locations = locations;
            model.Addresses = addresses;

            var temp = addresses.Select(o => new { o.Id, o.Receiver, o.AreaCode, o.Address, o.PostalCode, o.Phone, o.Mobile, o.IsDefault }).ToList();
            model.JsonData = JSON.Instance.ToJSON(temp, new JSONParameters { UseExtensions = false, EnableAnonymousTypes = true });

            return View(model);
        }

        public ActionResult Address(int? param)
        {
            var model = new UserAddress();
            var identity = User.Identity.SkIdentity();

            if (param.HasValue && param.Value > 0)
            {
                model = PassportService.GetUserAddress(param.Value, identity.Id);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Address(int? param, UserAddress userAddress)
        {
            var result = new SkResult<bool>();
            var identity = User.Identity.SkIdentity();
            if (param.HasValue && param.Value > 0)
            {
                var old = PassportService.GetUserAddress(param.Value, identity.Id);
                if (TryUpdateModel(old))
                {
                    old.IsDefault = userAddress.IsDefault;
                    result.Status = PassportService.UpdateUserAddress(old);
                }
            }
            else
            {
                userAddress.UserId = identity.Id;
                userAddress.CountryId = 86;
                userAddress.CreatedBy = User.Identity.Name;
                userAddress.CreatedOn = DateTime.Now;
                userAddress.Category = AddressCategory.ShippingAddress;

                result.Status = PassportService.CreateUserAddress(userAddress);
            }
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(int? param)
        {
            var result = new SkResult<string>(false);
            var identity = User.Identity.SkIdentity();

            if (param.HasValue && param.Value > 0)
            {
                var status = PassportService.DeleteUserAddress(param.Value, identity.Id);
                if (status)
                {
                    result.Status = true;
                }
                else
                {
                    result.Message = "删除失败，请重试！";
                }
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult SetDefault(int? param)
        {
            var result = new SkResult<string>(false);
            var identity = User.Identity.SkIdentity();

            if (param.HasValue && param.Value > 0)
            {
                var status = PassportService.SetDefaultUserAddress(param.Value, identity.Id);
                if (status)
                {
                    result.Status = true;
                }
                else
                {
                    result.Message = "设置失败，请重试！";
                }
            }
            return Json(result);
        }

    }
}
